<?php

namespace App\Http\Controllers\Admin;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\WithDraw;
use App\Models\WithdrawAccount;
use Illuminate\Support\Facades\Validator;
use App\Service\DrawService;
use App\Service\ApiService;
use Illuminate\Pagination\LengthAwarePaginator;
use Excel;

class DrawController extends Controller
{
	 public $v = 'admin.draw.';
	 
	 public $paginate = 20;
	 /**
	 * 提现记录
	 * @param Request $request
	 * @return type
	 */
	public function index(Request $request) {
		$stime = trim($request->query('stime'));
		$etime = trim($request->query('etime'));
		$status = trim($request->query('status'));
		
		$builder = new WithDraw();
		$query =  $builder->with(['drawaccount'=>function($query){
				$query->select('guser_id', 'name', 'alipay');
			}])
			->when($status, function ($query) use ($status) {
				$newstatus = $status == 3 ? 0 : $status;
				return $query->where('status', $newstatus);
			});
		if(!empty($stime) && !empty($etime)) {
			$etimes = $etime.' 23:59:59';
			$query = $query->whereBetween('created_at', [$stime, $etimes]);
		}
		$draw = $query->orderBy('id', 'desc')->paginate($this->paginate);
        return view($this->v . 'index', compact('stime', 'etime', 'status', 'draw'));
	}
	/**
	 * 审核页面
	 * @param type $id
	 * @return type
	 */
	public function edit($id)
    {
		$drawone = (new WithDraw())->with(['drawaccount'=>function($query){
				$query->select('guser_id', 'name', 'alipay');
			}])->where('id', $id)->first();
        return view($this->v . 'edit', compact('drawone'));
    }
	/**
	 * 审核
	 * @param type $id
	 * @return type
	 */
	 public function update(Request $request, $id)
    {
        $order_id = $request->input('order_id');
		$status = $request->input('status');
		$params = ['order_id' => $order_id, 'status' => $status];
        $validator = Validator::make($params, [
            'status' => 'required',
        ],[
			'status.required' => '请选择提现状态',	
        ]);		
        $error = $validator->errors()->first();
        if ($error) return ajaxError($error);
		if($status == 1) {
			if(empty($order_id)) {
				return ajaxError('订单号不能为空');
			}
		} 
        $res = (new DrawService())->UpdataStatus($params, $id);
        if (!$res) return ajaxError();
        return ajaxSuccess();
    }
	/**
	 * 提现账号支付宝绑定记录
	 * @param Request $request
	 * @return type
	 */
	public function alipay(Request $request) {
		$stime = trim($request->query('stime'));
		$etime = trim($request->query('etime'));
		$status = trim($request->query('status'));
		
		$builder = new WithdrawAccount();
		$query =  $builder
			->when($status, function ($query) use ($status) {
				$newstatus = $status == 3 ? 0 : $status;
				return $query->where('status', $newstatus);
			});
		if(!empty($stime) && !empty($etime)) {
			$etimes = $etime.' 23:59:59';
			$query = $query->whereBetween('created_at', [$stime, $etimes]);
		}
		$alipay = $query->orderBy('id', 'desc')->paginate($this->paginate);
        return view($this->v . 'alipay', compact('stime', 'etime', 'status', 'alipay'));
	}
	/**
	 * 支付宝绑定审核页面
	 * @param type $id
	 * @return type
	 */
	public function editalipay($id)
    {
		$alipayone = (new WithdrawAccount())->where('id', $id)->first();
        return view($this->v . 'editalipay', compact('alipayone'));
    }
	/**
	 * 支付宝绑定审核
	 * @param type $id
	 * @return type
	 */
	 public function updatealipay(Request $request, $id)
    {
		$status = $request->input('status');
		$params = ['status' => $status];
        $validator = Validator::make($params, [
            'status' => 'required',
        ],[
			'status.required' =>'请选择审核状态',	
        ]);
        $error = $validator->errors()->first();
        if ($error) return ajaxError($error);
        $res = (new DrawService())->UpdataAlipayStatus($params, $id);
		if($res['status'] == -1) {
			return ajaxError($res['error']);
		}
        if (!$res) return ajaxError();
        return ajaxSuccess();
    }
	/**
	 * 支付宝绑定修改页面
	 * @param type $id
	 * @return type
	 */
	public function upalipayone($id)
    {
		$alipayone = (new WithdrawAccount())->where('id', $id)->first();
        return view($this->v . 'upalipay', compact('alipayone'));
    }
	/**
	 * 支付宝绑定修改
	 * @param type $id
	 * @return type
	 */
	public function upalipay(Request $request, $id, DrawService $drawService, WithdrawAccount $withdrawAccountModels)
    {
		$params = $request->all();
		$res = $drawService->upDrawaAccount($params, $withdrawAccountModels, $id);
		if($res['status'] == -1) {
			return ajaxError($res['error']);
		} elseif($res['status'] == 0) {
			return ajaxError();
		}
		return ajaxSuccess();
	}
	/**
	 * 公会收益明细
	 * @param
	 * @return
	 */
	public function earning(Request $request, ApiService $apiService){
		$earning = [];
		$guild = $request->query('guild');
		$start_time = $request->query('start_time');
		//$perPage = 3;
        //当前页数 默认1
        $page = $request->page ?: 1;
        //计算每页分页的初始位置
        $offset = ($page * $this->paginate) - $this->paginate;
		$rest = $apiService->getEarning(['guild' => $guild, 'start_time' => $start_time]);
		if($rest['count'] != 0) {
			$earning = new LengthAwarePaginator(array_slice($rest['list'], $offset, $this->paginate, true), $rest['count'], $this->paginate,
				$page, ['path' => $request->url(), 'query' => $request->query()]);
		}
		$guildlist = $apiService->getGuildShow();
		return view($this->v . 'earning', compact('guildlist', 'guild', 'start_time', 'earning'));
	}
	/**
	 * 主播收益明细
	 * @param
	 * @return
	 */
	public function income(Request $request, ApiService $apiService){
		$income = [];
		$guild = $request->query('guild');
		$keyword = $request->query('keyword');
		$time_value = $request->query('time_value');
		//$perPage = 3;
        //当前页数 默认1
        $page = $request->page ?: 1;
        //计算每页分页的初始位置
        $offset = ($page * $this->paginate) - $this->paginate;
		$rest = $apiService->getIncome(['guild' => $guild, 'keyword' => $keyword, 'time_value' => $time_value]);
		if($rest['count'] != 0) {
			$income = new LengthAwarePaginator(array_slice($rest['list'], $offset, $this->paginate, true), $rest['count'], $this->paginate,
				$page, ['path' => $request->url(), 'query' => $request->query()]);
		}
		$guildlist = $apiService->getGuildShow();
		return view($this->v . 'income', compact('guildlist', 'guild', 'keyword', 'time_value', 'income'));
	}
	/**
	 * 主播收益明细导出
	 * @param
	 * @return
	 */
	public function exportlearning() {
	$cellData = [
            ['学号','姓名','成绩'],
			['10001','大幅度','99'],
			['10002','BBBBB','92'],
			['10003','CCCCC','95'],
			['10004','DDDDD','89'],
			['10005','EEEEE','96'],
        ];
        Excel::create("学生成绩",function ($excel) use ($cellData){
            $excel->sheet('score',function ($sheet) use ($cellData) {
                $sheet->rows($cellData);
				ob_end_clean();
            });
        })->export('csv');	
	}
}
